package com.sunfeng.chap2;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description:
 * a.txt 1234567890
 * b.txt abcdefg
 * @Date : 2021/10/9  14:36
 */
public class Test12_3 {
    public static void main(String[] args) throws IOException {
        test1();
    }

    /**
     * 如果count字节个数大于src.remaining,则通道的src.remaining字节个数传输到当前通道的position
     *  执行结果为: 1cdefg7890
     * @throws IOException
     */
    private static  void  test1() throws IOException {
        RandomAccessFile fileA = new RandomAccessFile("d:\\nio\\a.txt", "rw");
        RandomAccessFile fileB = new RandomAccessFile("d:\\nio\\b.txt", "rw");
        FileChannel channelA = fileA.getChannel();
        FileChannel channelB = fileB.getChannel();
        channelB.position(2);

        long transferFrom = channelA.transferFrom(channelB, 1, 200);

        System.out.println(transferFrom);
        channelB.close();
        channelA.close();
        fileB.close();
        fileA.close();
    }
    /**
     * 如果count字节个数小于等于src.remaining,则通道的count字节个数传输到当前通道的position
     *  执行结果为: 1cd4567890
     * @throws IOException
     */
    private static  void  test2() throws IOException {
        RandomAccessFile fileA = new RandomAccessFile("d:\\nio\\a.txt", "rw");
        RandomAccessFile fileB = new RandomAccessFile("d:\\nio\\b.txt", "rw");
        FileChannel channelA = fileA.getChannel();
        FileChannel channelB = fileB.getChannel();
        channelB.position(2);

        long transferFrom = channelA.transferFrom(channelB, 1, 2);

        System.out.println(transferFrom);
        channelB.close();
        channelA.close();
        fileB.close();
        fileA.close();
    }
}
